﻿using StripeDemo.Models;
using StripeNET;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;

namespace StripeDemo.Utils
{
    public class Util
    {
        // get all customer
        public static List<Customer> getAllCustomer()
        {
            StripeCustomerService customerService = new StripeCustomerService();
            IEnumerable<StripeCustomer> response = customerService.List();
            List<Customer> listCustomer = new List<Customer>();
            Parallel.ForEach(response, item =>
            {
                listCustomer.Add(convertToCustomer(item));
            });
            return listCustomer;
        }

        //get customer by extraInfo
        public static Customer getCustomerByExtraInfo(string extraInfo)
        {
            List<Customer> list = getAllCustomer();
            foreach (Customer item in list)
            {
                if (extraInfo.Equals(item.ExtraCustomerInfo))
                {
                    return item;
                }
            }
            return null;
        }

        // convert from StripeCard to card and set field default card
        public static Cards convertToCards(StripeCard stripeCard, string defaultid)
        {
            Cards card = convertToCards(stripeCard);
            if (card != null)
            {
                if (stripeCard.Id.Equals(defaultid))
                {
                    card.isDefault = "yes";
                }
            }
            return card;
        }

        // convert from StripeCard to card
        public static Cards convertToCards(StripeCard stripeCard)
        {
            Cards card = null; ;
            if (stripeCard != null)
            {
                card = new Cards();
                card.isDefault = "no";
                card.CardId = stripeCard.Id;
                card.Card = stripeCard.Brand;
                card.CardNumber = "**** **** **** " + stripeCard.Last4;
                card.CVC = stripeCard.CvcCheck;
                if (stripeCard.Metadata.ContainsKey(Constant.BILLING_ADDRESS))
                {
                    card.BillingAddress = stripeCard.Metadata[Constant.BILLING_ADDRESS];
                }
                card.ExpiresMonth = stripeCard.ExpirationMonth;
                card.ExpiresYear = stripeCard.ExpirationYear;
            }
            return card;
        }

        // get information of customer and put to view
        public static StripeCustomer getUserInfoById(string customerid)
        {
            try
            {
                StripeCustomerService customerService = new StripeCustomerService();
                return customerService.Get(customerid);
            }
            catch (StripeException exception)
            {
                return null;
            }
        }

        //convert from StripeCustomer to Customer
        public static Customer convertToCustomer(StripeCustomer stripecustomer)
        {
            Customer customer = new Customer();
            customer.CustomerId = stripecustomer.CustomerId;
            customer.Email = stripecustomer.Email;
            customer.DefaultCard = stripecustomer.DefaultSourceId;
            Dictionary<string, string> metadata = stripecustomer.Metadata;
            if (stripecustomer.StripeShipping == null)
            {
                customer.BillingAddress = "";
                customer.FullName = "";
                customer.PhoneNumber = "";
            }
            else {
                if (stripecustomer.StripeShipping.BillingAddress != null)
                {
                    customer.BillingAddress = stripecustomer.StripeShipping.BillingAddress.Line1;
                }
                else {
                    customer.BillingAddress = "";
                }
                customer.FullName = stripecustomer.StripeShipping.FullName;
                customer.PhoneNumber = stripecustomer.StripeShipping.Phone;
            }
            if (metadata.ContainsKey(Constant.EXTRA_CUSTOMER_INFO))
            {
                customer.ExtraCustomerInfo = metadata[Constant.EXTRA_CUSTOMER_INFO];
            }
            return customer;
        }

    }
}